<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\AccountBook;
use Hyperf\DbConnection\Db;

class AccountBookController extends AbstractController
{
    // 录入金额
    public function add()
    {
        // 金额
        $money = $this->request->input('money', 0.0);
        // 分类   
        $category_id = $this->request->input('category_id',0)[1];
        // 时间
        $create_at = $this->request->input('create_at',0);
        $account_book = new AccountBook();

        $account_book->money =$money;
        $account_book->category_id =$category_id;
        $account_book->create_at =$create_at;
        $account_book->save();
        $data = [
            'key' => 'value'
        ];
        return $this->response->json($data);
        
    }
    //查询记账本
    public function list()
    {
        $pagintor = AccountBook::paginate(10);
        $data = [
            'data' => $pagintor,
            'count' => AccountBook::query()->count(),
            'status' => 1
        ];
        return $this->response->json($data);
    }
    // 周报
    public function chartsBYWeek()
    {
        $account_by_week = Db::table('account_book')->groupBy("week(date_add(create_at,interval 6 day),2)")->select('yearweek(create_at,1) as week_num',Db::raw('SUM(money) as total_money'))->orderByRaw('create_at')->get();
        $data=array();
        $date=array();
        foreach($account_by_week as $item){
            array_push($data,$item['total_money']);
            array_push($date,$item['week_num']);
        }
        $data = [
            'data' => $data,
            'date' => $date,
            'status' => 1
        ];
        return $this->response->json($data);
    }
    // 月报
    public function chartsBYMonth()
    {
        $account_by_month = Db::table('account_book')->groupBy("DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m')")->select("DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m') as year_month",Db::raw('SUM(money) as total_money'))->orderByRaw('create_at')->get();
        $data=array();
        $date=array();
        foreach($account_by_month as $item){
            array_push($data,$item['total_money']);
            array_push($date,$item['year_month']);
        }
        $data = [
            'data' => $data,
            'date' => $date,
            'status' => 1
        ];
        return $this->response->json($data);
    }
    // 年报
    public function chartsBYYear()
    {
        $account_by_year = Db::table('account_book')->groupBy("year(create_at)")->select("year(create_at) as year",Db::raw('SUM(money) as total_money'))->orderByRaw('create_at')->get();
        $data=array();
        $date=array();
        foreach($account_by_year as $item){
            array_push($data,$item['total_money']);
            array_push($date,$item['year']);
        }
        $data = [
            'data' => $data,
            'date' => $date,
            'status' => 1
        ];
        return $this->response->json($data);
    }
}
